#include "qformwidgetview.h"

#include "qformlistwidget.h"
#include "qformwidget.h"
#include "qremoveformdialog.h"
#include "qnewpagedialog.h"
#include "qtemplatedialog.h"

#include "../../libs/corelib/styledbar.h"
#include "../../libs/sharedLib/qprojectcore.h"
#include "../../libs/coreLib/qactiontoolbar.h"
#include "../../libs/sharedLib/qlanguagemanager.h"
#include "../../libs/sharedLib/qpagemanager.h"
#include "../../libs/coreLib/qsoftcore.h"
#include "../../libs/sharedLib/qpageinfo.h"

#include <QVBoxLayout>
#include <QStringList>

QFormWidgetView::QFormWidgetView(QWidget *parent) :
    QWidget(parent),
    m_formWidget(new QFormListWidget),
    m_styledBar(new CORELIB::StyledBar),
    m_languageComboBox(new QComboBox(this))
{
    QVBoxLayout *l=new QVBoxLayout;
    l->setMargin(0);
    l->setSpacing(0);

    l->addWidget(m_styledBar);
    l->addWidget(m_formWidget);

    this->setLayout(l);
    connect(m_formWidget,SIGNAL(selectWidget(QWidget*)),this,SIGNAL(selectWidget(QWidget*)));
    connect(m_formWidget,SIGNAL(showFormSignal(QWidget*)),this,SIGNAL(showFormSignal(QWidget*)));
    connect(m_formWidget,SIGNAL(need_make_page_position()),this,SIGNAL(need_make_page_position()));

    SHAREDLIB::QProjectCore *c=SHAREDLIB::QProjectCore::getCore();
    connect(c,SIGNAL(projectOpen()),this,SLOT(projectOpen()));
    connect(c,SIGNAL(projectClosed()),this,SLOT(projectClosed()));

    CORELIB::QSoftCore *core=CORELIB::QSoftCore::getCore();

    CORELIB::QActionToolBar *toolbar=new CORELIB::QActionToolBar;
    QList<QAction*> list;
    QAction *ac;

    ac=core->action("FormManager-Add");
    if(ac!=NULL)
    {
        list.append(ac);
        connect(ac,SIGNAL(triggered()),this,SLOT(add()));
    }

    ac=core->action("FormManager-Del");
    if(ac!=NULL)
    {
        list.append(ac);
        connect(ac,SIGNAL(triggered()),this,SLOT(remove()));
    }

    ac=new QAction(this);
    ac->setSeparator(true);
    list.append(ac);

    ac=core->action("FormManager-Left");
    if(ac!=NULL)
    {
        list.append(ac);
        connect(ac,SIGNAL(triggered()),this,SLOT(left()));
    }

    ac=core->action("FormManager-V-Center");
    if(ac!=NULL)
    {
        list.append(ac);
        connect(ac,SIGNAL(triggered()),this,SLOT(v_center()));
    }

    ac=core->action("FormManager-Right");
    if(ac!=NULL)
    {
        list.append(ac);
        connect(ac,SIGNAL(triggered()),this,SLOT(right()));
    }

    ac=core->action("FormManager-Top");
    if(ac!=NULL)
    {
        list.append(ac);
        connect(ac,SIGNAL(triggered()),this,SLOT(top()));
    }

    ac=core->action("FormManager-H-Center");
    if(ac!=NULL)
    {
        list.append(ac);
        connect(ac,SIGNAL(triggered()),this,SLOT(h_center()));
    }


    ac=core->action("FormManager-Bottom");
    if(ac!=NULL)
    {
        list.append(ac);
        connect(ac,SIGNAL(triggered()),this,SLOT(bottom()));
    }

    ac=new QAction(this);
    ac->setSeparator(true);
    list.append(ac);

    ac=core->action("FormManager-Same-Width");
    if(ac!=NULL)
    {
        list.append(ac);
        connect(ac,SIGNAL(triggered()),this,SLOT(sameWidth()));
    }

    ac=core->action("FormManager-Same-Height");
    if(ac!=NULL)
    {
        list.append(ac);
        connect(ac,SIGNAL(triggered()),this,SLOT(sameHeight()));
    }

    ac=core->action("FormManager-Same-Gemotry");
    if(ac!=NULL)
    {
        list.append(ac);
        connect(ac,SIGNAL(triggered()),this,SLOT(sameGemotry()));
    }

    ac=new QAction(this);
    ac->setSeparator(true);
    list.append(ac);

    toolbar->addButtonActions(list);

    QHBoxLayout *h=new QHBoxLayout;

    m_languageComboBox->setFixedWidth(150);
    h->setMargin(0);
    h->setSpacing(0);
    h->addWidget(toolbar,1);
    h->addWidget(m_languageComboBox,0);
    m_styledBar->setLayout(h);

    connect(m_formWidget,SIGNAL(formAddSignal(QWidget*)),this,SIGNAL(formAddSignal(QWidget*)));
    connect(m_formWidget,SIGNAL(formAddWidget(QWidget*,int)),this,SIGNAL(addWidgetSignal(QWidget*,int)));
    connect(m_formWidget,SIGNAL(widgetRemove(QWidget*)),this,SIGNAL(widgetRemove(QWidget*)));
}

void QFormWidgetView::showForm(SHAREDLIB::QPageInfo *info)
{
    m_formWidget->showForm(info);
}

void QFormWidgetView::widgetSelect(QWidget *wid)
{
    QWidget* parent=NULL;
    QWidget* temp=wid->parentWidget();
    while(temp!=NULL)
    {
        if(temp->inherits("QFormWidget"))
        {
            parent=temp;
            break;
        }
        temp=temp->parentWidget();
    }
    QFormWidget*    form=m_formWidget->currentForm();
    if(parent==NULL)
    {
        if(form!=wid)
        {
            showForm(m_formWidget->pageInfo((QFormWidget*)wid));
        }
        else
        {
            form->selectThis();
        }
    }
    else
    {
        if(form==parent)
        {
            form->clearSelect();
            form->selectWidget(wid);
        }
        else
        {
            showForm(m_formWidget->pageInfo((QFormWidget*)parent));
            ((QFormWidget*)parent)->selectWidget(wid);
        }
    }
}


void QFormWidgetView::projectClosed()
{
    SHAREDLIB::QProjectCore*    c=SHAREDLIB::QProjectCore::getCore();
    disconnect(c->languageManager(),SIGNAL(currentLanguageChanged(QObject*)),this,SLOT(languageChanged()));
    disconnect(c->pageManager(),SIGNAL(currentFormChanged()),this,SLOT(currentFormChanged()));
    disconnect(m_languageComboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(languageSelectChanged(QString)));
    disconnect(c->languageManager(),SIGNAL(refresh()),this,SLOT(refreshLanguage()));
    disconnect(c->pageManager(),SIGNAL(refresh()),this,SLOT(refreshForm()));
    emit clear_object_list();
    m_formWidget->clear();
    m_languageComboBox->clear();
}

void QFormWidgetView::projectOpen()
{
    SHAREDLIB::QProjectCore*    c=SHAREDLIB::QProjectCore::getCore();
    m_languageComboBox->addItems(c->languageManager()->languages());
    QObject *obj=c->languageManager()->getCurrentLanguage();
    if(obj!=NULL)
    {
        m_languageComboBox->setCurrentIndex(m_languageComboBox->findText(obj->property("languageName").toString()));
    }
    connect(c->languageManager(),SIGNAL(currentLanguageChanged(QObject*)),this,SLOT(languageChanged()));
    connect(m_languageComboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(languageSelectChanged(QString)));
    connect(c->languageManager(),SIGNAL(refresh()),this,SLOT(refreshLanguage()));

    connect(c->pageManager(),SIGNAL(currentFormChanged()),this,SLOT(currentFormChanged()));
    connect(c->pageManager(),SIGNAL(refresh()),this,SLOT(refreshForm()));
}

void QFormWidgetView::languageChanged()
{
    SHAREDLIB::QProjectCore*    c=SHAREDLIB::QProjectCore::getCore();
    disconnect(c->languageManager(),SIGNAL(currentLanguageChanged(QObject*)),this,SLOT(languageChanged()));
    disconnect(m_languageComboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(languageSelectChanged(QString)));
    QObject *obj=c->languageManager()->getCurrentLanguage();
    if(obj!=NULL)
    {
        m_languageComboBox->setCurrentIndex(m_languageComboBox->findText(obj->property("languageName").toString()));
    }
    connect(c->languageManager(),SIGNAL(currentLanguageChanged(QObject*)),this,SLOT(languageChanged()));
    connect(m_languageComboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(languageSelectChanged(QString)));
}

void QFormWidgetView::currentFormChanged()
{
    SHAREDLIB::QProjectCore*    c=SHAREDLIB::QProjectCore::getCore();
    disconnect(c->pageManager(),SIGNAL(currentFormChanged()),this,SLOT(currentFormChanged()));
    SHAREDLIB::QPageInfo* info=SHAREDLIB::QProjectCore::getCore()->pageManager()->getCurrentPage();
    if(info!=NULL)
    {
        showForm(info);
    }
    connect(c->pageManager(),SIGNAL(currentFormChanged()),this,SLOT(currentFormChanged()));
}

void QFormWidgetView::languageSelectChanged(const QString &name)
{
    SHAREDLIB::QProjectCore*    c=SHAREDLIB::QProjectCore::getCore();
    disconnect(c->languageManager(),SIGNAL(currentLanguageChanged(QObject*)),this,SLOT(languageChanged()));
    disconnect(m_languageComboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(languageSelectChanged(QString)));
    c->languageManager()->setCurrentLanguage(name);
    connect(c->languageManager(),SIGNAL(currentLanguageChanged(QObject*)),this,SLOT(languageChanged()));
    connect(m_languageComboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(languageSelectChanged(QString)));
}

void QFormWidgetView::refreshLanguage()
{
    SHAREDLIB::QProjectCore*    c=SHAREDLIB::QProjectCore::getCore();
    disconnect(c->languageManager(),SIGNAL(currentLanguageChanged(QObject*)),this,SLOT(languageChanged()));
    disconnect(m_languageComboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(languageSelectChanged(QString)));
    m_languageComboBox->clear();
    m_languageComboBox->addItems(c->languageManager()->languages());
    QObject *obj=c->languageManager()->getCurrentLanguage();
    if(obj!=NULL)
    {
        m_languageComboBox->setCurrentIndex(m_languageComboBox->findText(obj->property("languageName").toString()));
    }
    connect(c->languageManager(),SIGNAL(currentLanguageChanged(QObject*)),this,SLOT(languageChanged()));
    connect(m_languageComboBox,SIGNAL(currentIndexChanged(QString)),this,SLOT(languageSelectChanged(QString)));
}

void QFormWidgetView::refreshForm()
{
    SHAREDLIB::QProjectCore*    c=SHAREDLIB::QProjectCore::getCore();
    disconnect(c->pageManager(),SIGNAL(currentFormChanged()),this,SLOT(currentFormChanged()));
    showForm(c->pageManager()->getCurrentPage());
    connect(c->pageManager(),SIGNAL(currentFormChanged()),this,SLOT(currentFormChanged()));
}

void QFormWidgetView::add()
{
    QNewPageDialog dlg(this);
    dlg.exec();

    int ret=dlg.get_ret();

    QStringList list;
    list<<"form"<<"dialog"<<"keyboard";
    if(ret>0 && ret<=list.size())
    {
        QTemplateDialog dialog(list.at(ret-1),this);
        dialog.exec();
        QString name=dialog.get_name();
        if(name!="")
        {
            SHAREDLIB::QProjectCore::getCore()->addNewPage(list.at(ret-1),name,dialog.get_template_name());
        }
    }
}

void QFormWidgetView::remove()
{
    QRemoveFormDialog dlg(this);
    dlg.exec();
}

void QFormWidgetView::left()
{
    m_formWidget->left();
}

void QFormWidgetView::h_center()
{
    m_formWidget->h_center();
}

void QFormWidgetView::right()
{
    m_formWidget->right();
}

void QFormWidgetView::top()
{
    m_formWidget->top();
}

void QFormWidgetView::v_center()
{
    m_formWidget->v_center();
}

void QFormWidgetView::bottom()
{
    m_formWidget->bottom();
}

void QFormWidgetView::sameWidth()
{
    m_formWidget->sameWidth();
}

void QFormWidgetView::sameHeight()
{
    m_formWidget->sameHeight();
}

void QFormWidgetView::sameGemotry()
{
    m_formWidget->sameGemotry();
}

void QFormWidgetView::make_page_position(QVariant &info)
{
    m_formWidget->make_page_position(info);
}

void QFormWidgetView::set_page_position(const QVariant &info)
{
    m_formWidget->set_page_position(info);
}

bool QFormWidgetView::page_position(const QVariant &info)
{
    return m_formWidget->page_position(info);
}
